package com.example.occlusiondemo;

import com.supermap.ar.areffect.EffectView;
import com.supermap.ar.areffect.utils.ScreenPointTool;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 屏幕坐标管理器
 * <p>用于裁剪</p>
 * <pre>
 *     多种方式set，
 *     仅遮挡计算时get
 * </pre>
 *
 * @author tanyx 2022/9/20
 * @version 1.0
 **/
public class ScreenPointManager {
    private List<ArrayList<int[]>> clippingPoint;
    private volatile static ScreenPointManager screenPointManager = null;
    private List<ArrayList<int[]>> points2;

    public interface OnUpdatePointListener{
        void update(List<ArrayList<int[]>> points,List<ArrayList<int[]>> points2);
    }

    private OnUpdatePointListener onUpdatePointListener;

    public void setOnUpdatePointListener(OnUpdatePointListener onUpdatePointListener) {
        this.onUpdatePointListener = onUpdatePointListener;
    }

    private ScreenPointManager() {
        clippingPoint = new ArrayList();

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    if (onUpdatePointListener!=null){
                        onUpdatePointListener.update(clippingPoint,points2);
                    }
                }catch (Exception e){

                }
            }
        },33,33);
    }

    public static ScreenPointManager getInstance(){
        if (screenPointManager == null){
            synchronized (ScreenPointManager.class){
                if (screenPointManager == null){
                    screenPointManager = new ScreenPointManager();
                }
            }
        }
        return screenPointManager;
    }

    /**
     * 设置点集
     * <pre>
     *     备注：多个地方set<br/>
     * {@link com.example.occlusiondemo.layout.func.RollingFuncLayout}中实时计算
     *
     * </pre>
     * @param points
     * @param points2 底部点转的屏幕坐标
     */
    public void setClippingPoint(List<ArrayList<int[]>> points, List<ArrayList<int[]>> points2){
        synchronized (ScreenPointManager.class){
            this.clippingPoint = points;
            this.points2 = points2;
//            if (onUpdatePointListener != null){
//                onUpdatePointListener.update(this.clippingPoint,points2);
//            }
        }
    }

    /**
     * 设置默认的裁剪点集
     * <p>默认不裁剪</p>
     */
    public void setDefaultClippingPoint(EffectView effectView){
        synchronized (ScreenPointManager.class){
            ArrayList<int[]> ints = new ArrayList<>();
            ints.add(new int[]{0,0});
            ints.add(new int[]{effectView.getWidth(),0});
            ints.add(new int[]{effectView.getWidth(),effectView.getHeight()});
            ints.add(new int[]{0,effectView.getHeight()});
            ints.add(new int[]{0,0});
            this.clippingPoint.clear();
            this.clippingPoint.add(ints);
//            if (onUpdatePointListener!=null){
//                onUpdatePointListener.update(this.clippingPoint,null);
            this.points2 = null;
//            }
        }
    }
}
